﻿using BZ.Logger;
using HalconDotNet;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.MachineManager;
using NDK.Motion.Monitor;
using NDK.Motion.Sys;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
using System.Windows;

namespace NDK.Module.StationLogic
{
    public class Station_下置激光 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_下置激光 Instance = new Station_下置激光();
        public Stopwatch SunWay11 = new Stopwatch();
        public string StationName = "Station11下置激光";

        public bool IsHaveProduct;

        //步骤字段、属性
        private StationStep _Step = StationStep.轴移动至扫描起始位;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if (_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init()
        {
            base.Init();
            IsHaveProduct = false;
            CurHoleId = 9;
            StationID = 9;
            logBaseInfo = "下置激光流程";
            QueueSN = new ConcurrentQueue<string>();
            Step = StationStep.轴移动至扫描起始位;
            QueueImage = new ConcurrentQueue<MeasureImage>();

            VisionFlowName = $"下Laser_{Common.CurProType}";
            SunWay11.Start();
        }

        public override void ActionProcess()
        {
            try
            {
                switch (Step)
                {
                    case StationStep.轴移动至扫描起始位:
                        if (MotionControl.AxesMove(Machine.pos_激光扫描起始位_下激光工位))
                        {
                            SignalComplete = true;
                            Step = StationStep.等待上工位取料;
                        }
                        break;

                    case StationStep.等待上工位取料:
                        if (Station_转盘下料.Instance.SignalRequest)
                        {
                            SignalComplete = false;
                            Step = StationStep.等待上工位有料;
                        }
                        break;

                    case StationStep.等待上工位有料:
                        if (Station_转盘下料.Instance.SignalComplete)
                        {
                            SignalRequest = true;
                            Step = StationStep.等待上工位放料完成;
                        }
                        break;

                    case StationStep.等待上工位放料完成:
                        if (!Station_转盘下料.Instance.SignalComplete)
                        {
                            SignalRequest = false;
                            Step = IsHaveProduct ? StationStep.触发激光开始扫描 : StationStep.轴移动至扫描起始位;
                        }
                        break;

                    case StationStep.触发激光开始扫描:
                        if (!Common.IsEmptyModel)
                        {
                            if (Common.SSZN_下激光.Start())
                            {

                            }
                            else
                            {
                                MessageBox.Show("下激光打开失败");
                                MachineStatusManager.CurrentMachineStatus = MachineStatus.Alarm;
                                break;
                            }
                        }
                        Step = StationStep.轴移动至扫描终点位;
                        break;

                    case StationStep.轴移动至扫描终点位:
                        if (MotionControl.AxesMove(Machine.pos_激光扫描终点位_下激光工位))
                        {
                            Step = Common.IsEmptyModel ? StationStep.调用算法计算 : StationStep.获取激光扫描图片;
                        }
                        //SignalComplete = true;
                        break;

                    case StationStep.获取激光扫描图片:
                        var himage = Common.SSZN_下激光.HeightImage(3000);
                        if (himage != null)
                        {
                            Station_转盘七穴.Instance.QueueSN.TryDequeue(out string sn);
                            QueueSN.Enqueue(sn);
                            Station_转盘八穴.Instance.TurntableId.TryDequeue(out int id);
                            var images = new (string, HImage)[] { ("下激光", himage) };
                            QueueImage.Enqueue(new MeasureImage() { Hole = id, Images = images });
                            DataProcess.Instance.VisionProcessAsync($"下Laser_{Common.CurProType}", $"下Laser_{Common.CurProType}Data", id, sn, images);
                            Step = StationStep.调用算法计算;
                        }
                        else
                        {
                            Common.SSZN_下激光.StopAcqHeightImage();
                            Common.SSZN_下激光.Connect(Common.SP_深视下激光IP);
                            AlarmPause("激光出图异常", "下置激光出图异常", "请检查", "重试");

                            if (MotionControl.AxesMove(Machine.pos_激光扫描起始位_下激光工位))
                            {
                                Step = StationStep.触发激光开始扫描;
                            }
                        }
                        Common.SSZN_下激光.Stop();
                        SignalComplete = true;
                        break;

                    case StationStep.调用算法计算:
                        if (Common.GRR循环模式)
                        {
                            GRRSignalComplete = true;
                        }
                        Step = StationStep.轴移动至扫描起始位;
                        break;

                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess()
        {
            ActionProcess();
        }

        public override void GRRActionProcess()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            轴移动至扫描起始位,
            等待上工位取料,
            等待上工位有料,
            等待上工位放料完成,
            触发激光开始扫描,
            轴移动至扫描终点位,
            获取激光扫描图片,
            调用算法计算,
        }
    }

}
